//
//  HWQCJiaoYiTableViewCell.m
//  HWanGame
//
//  Created by 陈道斌 on 2019/9/9.
//  Copyright © 2019 Chendaobin. All rights reserved.
//

#import "HWQCJiaoYiTableViewCell.h"
#import "HWClinchDealCell.h"
#import "HWAccountDetailsViewController.h"
#import "HWViewUtils.h"

@interface HWQCJiaoYiTableViewCell()<UITableViewDelegate,UITableViewDataSource>
@property(strong,nonatomic) UITableView *maintablview;
@property(strong,nonatomic) UIImageView *shujuimagvie;
@property(strong,nonatomic) UILabel *zhawushujuLabel;

@property(strong,nonatomic) NSMutableArray *shujuMuArr;
@property(strong,nonatomic) NSString *p;
@property(strong,nonatomic) NSString *max_p;
@end


@implementation HWQCJiaoYiTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    //@property(strong,nonatomic) UITableView *maintablview;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupUI];
    }
    return  self;
}


- (void)setupUI {
    
    
    self.shujuimagvie = [[UIImageView alloc]initWithFrame:CGRectMake((kScreenW-188)/2, 33, 188, 124)];
    self.shujuimagvie.image = [UIImage imageNamed:@"details_dealbg"];
 //   self.shujuimagvie.hidden = YES;
    
    [self.contentView addSubview: self.shujuimagvie];
    self.zhawushujuLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 124+33+9, kScreenW, 12)];
    self.zhawushujuLabel.text = @"暂无数据";
  //   self.shujuimagvie.hidden = YES;
    self.zhawushujuLabel.font = [UIFont systemFontOfSize:12];
    self.zhawushujuLabel.textAlignment = NSTextAlignmentCenter;
    self.zhawushujuLabel.textColor = RGB(102, 102, 102);
    [self.contentView  addSubview:self.zhawushujuLabel];
    
    
    
    
    
    self.maintablview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenW,  kScreenH - (StatusBarHeight+17*3+196+5+44+TabbarSafeBottomMargin)) style:UITableViewStylePlain];
    self.maintablview.delegate = self;
    self.maintablview.dataSource = self;
    self.maintablview.tag = 10;
  //  self.maintablview.scrollEnabled = NO;
    self.maintablview.separatorStyle =  UITableViewCellSeparatorStyleNone;
    self.maintablview.showsVerticalScrollIndicator = NO;
    self.maintablview.tableFooterView = [[UIView alloc]init];
    self.maintablview.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.maintablview];
    
     [self addrefreshControl];
    
    
    
}

- (void)setGameId:(NSString *)gameId {
    _gameId   = gameId;
    
   
    [self.maintablview.mj_header  beginRefreshing];
    
    
    
    
}

- (void)addrefreshControl {
    
    self.maintablview.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(underRefresh)];
   
    self.maintablview.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(onRefresh)];
    
    
}


// 下拉加载
- (void)underRefresh {
    
    
       self.p = @"1";
      NSDictionary *parametes  = nil;
      if ([[HWLocallyStoredData getLoginUserID] isEqualToString:@""]) {
          
          parametes = @{
                                                    @"id" : self.gameId,
                                                    @"p" : self.p,
                                                    @"ios" : @"1",
                                                    @"ct" :  @"app",
                                                    @"ac" : @"game_info3"
                                                    };
          
          
      } else {
                parametes = @{
                                              @"id" : self.gameId,
                                              @"p" : self.p,
                                              @"ios" : @"1",
                                              @"ct" : @"app",
                                              @"ac" : @"game_info3"
                                              };
          
          
          
          
      }
         
       
      self.shujuMuArr = [[NSMutableArray alloc]init];
    
        
      [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
            
           [self.maintablview.mj_header endRefreshing];
           [self.maintablview.mj_footer endRefreshing];
          
          
            
            NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
            
            if ([codeStr intValue] == 1) {
                NSLog(@"%@",response);
               
               
                NSMutableArray *arr   =  response[@"data"];
                
                for (int i = 0 ; i < arr.count; i++) {
                    [self.shujuMuArr addObject:arr[i]];

                }
                
                
                self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
                self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
                [self.maintablview reloadData];
                
                

            } else {
                
                
                [self.maintablview.mj_header endRefreshing];
                [self.maintablview.mj_footer endRefreshing];
                
                
                [MBProgressHUD showError:response[@"message"]];
                
            }
         
            
            
        } failure:^(NSString *error) {
          
            [self.maintablview.mj_header endRefreshing];
            [self.maintablview.mj_footer endRefreshing];
        }];
      
    
    
    
    
    
    
}

//上拉加载
- (void)onRefresh {
    
    
        NSDictionary  *parametes = @{
                                                     @"id" : self.gameId,
                                                     @"p" : self.p,
                                                     @"ios" : @"1",
                                                     @"ct" : @"app",
                                                     @"ac" : @"game_info3"
                                                     };
    
    
     int p = [self.p intValue];
                   
     int max_p = [self.max_p intValue];
                   
      if (p > max_p) {
          
          [MBProgressHUD showError:@"没有更多数据，请待会在上拉加载"];
                       
            return ;
                       
        }
    
    
      [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
            
           [self.maintablview.mj_header endRefreshing];
           [self.maintablview.mj_footer endRefreshing];
          
          
            
            NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
            
            if ([codeStr intValue] == 1) {
                NSLog(@"%@",response);
               
               
                 NSMutableArray *arr   =  response[@"data"];
                
                for (int i = 0 ; i < arr.count; i++) {
                    [self.shujuMuArr addObject:arr[i]];

                }
            
                self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
                self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
                [self.maintablview reloadData];
                
        
          
            } else {
                
                
                [self.maintablview.mj_header endRefreshing];
                [self.maintablview.mj_footer endRefreshing];
                
                
                [MBProgressHUD showError:response[@"message"]];
                
            }
         
            
            
        } failure:^(NSString *error) {
          
            [self.maintablview.mj_header endRefreshing];
            [self.maintablview.mj_footer endRefreshing];
        }];
      
    
    
    
    
}




#pragma mark -------- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return  self.shujuMuArr.count;
    
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellID = @"cellID";
    HWClinchDealCell *cell =  [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell =  [[NSBundle mainBundle]loadNibNamed:@"HWClinchDealCell" owner:nil options:nil].lastObject;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor whiteColor];
    NSDictionary *dic = self.shujuMuArr[indexPath.row];
    cell.dataDic = dic;
    
    
    return  cell;
    
}


#pragma  mark -------------- UITableViewDelegate


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    return  86;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
     NSDictionary *dic = self.shujuMuArr[indexPath.row];
    NSLog(@"数据 ： %@",dic);
    
    HWAccountDetailsViewController *alloun = [[HWAccountDetailsViewController alloc]init];
   // []
    alloun.idStr = dic[@"id"];
    [[HWViewUtils getCurrentVC].navigationController pushViewController:alloun animated:YES];
    
    
    
    
}









@end
